﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Web.UI;

namespace Beetle.Web
{
    /// <summary>
    /// 帮助类
    /// </summary>
    /// <!-- 作者:韦腾  时间:2010年02月09日 -->
    public static class Helper
    {
        public static void RegisterWebFormsScript(System.Web.UI.Page page)
        {
            Type pageType = typeof(System.Web.UI.Page);
            MethodInfo method = pageType.GetMethod("RegisterWebFormsScript", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            method.Invoke(page, null);
        }

        public static void RegisterPostBackScript(System.Web.UI.Page page)
        {
            Type pageType = typeof(System.Web.UI.Page);
            MethodInfo method = pageType.GetMethod("RegisterPostBackScript", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            method.Invoke(page, null);
        }

        public static void RegisterEnabledControl(System.Web.UI.Page page, Control control)
        {
            Type pageType = typeof(System.Web.UI.Page);
            MethodInfo method = pageType.GetMethod("RegisterEnabledControl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            method.Invoke(page, new object[] { control });
        }

        public static void RegisterFocusScript(System.Web.UI.Page page)
        {
            Type pageType = typeof(System.Web.UI.Page);
            MethodInfo method = pageType.GetMethod("RegisterFocusScript", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            method.Invoke(page, null);
        }

        public static string DecryptString(string decryptString)
        {
            if (string.IsNullOrEmpty(decryptString))
            {
                return decryptString;
            }

            Type pageType = typeof(System.Web.UI.Page);
            MethodInfo method = pageType.GetMethod("DecryptString", BindingFlags.DeclaredOnly |BindingFlags.NonPublic |BindingFlags.Static | BindingFlags.InvokeMethod);
            object obj = method.Invoke(null, new object[] { decryptString });

            if (obj == null)
            {
                return null;
            }

            return obj.ToString();
        }

        public static string MergeScript(string firstScript, string secondScript)
        {
            if (!string.IsNullOrEmpty(firstScript))
            {
                return (firstScript + secondScript);
            }
            if (secondScript.TrimStart(new char[0]).StartsWith("javascript:", StringComparison.Ordinal))
            {
                return secondScript;
            }

            return ("javascript:" + secondScript);
        }

        /// <summary>
        /// 确保以分号结尾
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EnsureEndWithSemiColon(string value)
        {
            if (value != null)
            {
                int length = value.Length;
                if ((length > 0) && (value[length - 1] != ';'))
                {
                    return (value + ";");
                }
            }
            return value;
        }


    }
}
